/*
@title: disco
@description: "Disco" is a rhythm-based game where players assume the role of a DJ, attempting to hit corresponding keys as arrows move down the screen towards a DJ controller. Players need to quickly match blue arrows with WASD keys and red arrows with IJKL keys, while the pace intensifies over time. Achieving the highest score involves precise timing and avoiding incorrect or missed inputs.
@author: rakshit rangaprasad
@tags: []
@addedOn: 2023-03-05

How to Play:
Watch as the arrows move down the screen. Once the arrows hit the DJ controller,
hit the corresponding key. The blue arrows are for WASD, and the red arrows
are for IJKL. It may seem easy at first, but watch out! The arrows move faster
as the song goes on. You lose points if you hit the wrong key, and you don't get
any points if you miss a key. Good luck, and see how many points you can get!
*/
 
const w = "w",
  a = "a",
  s = "s",
  d = "d",
  i = "i",
  j = "j",
  k = "k",
  l = "l",
  row = "R",
  blue = "B",
  red = "C",
  black = "K"

setLegend(
  [ w, bitmap`
................
................
.......00.......
......0000......
.....007700.....
....00777700....
....07777770....
....00077000....
......0770......
......0770......
......0770......
......0770......
......0000......
................
................
................` ],
  [ a, bitmap`
................
................
................
................
.....000........
....0070........
...0077000000...
..00777777770...
..00777777770...
...0077000000...
....0070........
.....000........
................
................
................
................` ],
  [ s, bitmap`
................
................
................
......0000......
......0770......
......0770......
......0770......
......0770......
....00077000....
....07777770....
....00777700....
.....007700.....
......0000......
.......00.......
................
................` ],
  [ d, bitmap`
................
................
................
................
........000.....
........0700....
...0000007700...
...07777777700..
...07777777700..
...0000007700...
........0700....
........000.....
................
................
................
................` ],
  [ i, bitmap`
................
................
.......00.......
......0000......
.....003300.....
....00333300....
....03333330....
....00033000....
......0330......
......0330......
......0330......
......0330......
......0000......
................
................
................` ],
  [ j, bitmap`
................
................
................
................
.....000........
....0030........
...0033000000...
..00333333330...
..00333333330...
...0033000000...
....0030........
.....000........
................
................
................
................` ],
  [ k, bitmap`
................
................
................
......0000......
......0330......
......0330......
......0330......
......0330......
....00033000....
....03333330....
....00333300....
.....003300.....
......0000......
.......00.......
................
................` ],
  [ l, bitmap`
................
................
................
................
........000.....
........0300....
...0000003300...
...03333333300..
...03333333300..
...0000003300...
........0300....
........000.....
................
................
................
................` ],
  [ row, bitmap`
1111111111111111
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1111111111111111` ],
  [ red, bitmap`
1111111111111111
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1333333333333331
1111111111111111`],
  [ blue, bitmap`
1111111111111111
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1777777777777771
1111111111111111`],
  [ black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000` ]
)

const music = {
  "w": tune`
500: F4^500,
15500`,
  "a": tune`
500: G4^500,
15500`,
  "s": tune`
500: A4^500,
15500`,
  "d": tune`
500: B4^500,
15500`,
  "i": tune`
500: C5^500,
15500`,
  "j": tune`
500: D5^500,
15500`,
  "k": tune`
500: E5^500,
15500`,
  "l": tune`
500: F5^500,
15500`,
  gameOver: tune`
333.3333333333333: C5-333.3333333333333,
333.3333333333333: E5-333.3333333333333,
333.3333333333333: G5-333.3333333333333,
333.3333333333333: G5-333.3333333333333,
333.3333333333333: C5-333.3333333333333,
9000`
}

let level = 0
const maps = {
  game: map`
..........
..........
..........
..........
..........
..........
RRRRBCRRRR
..........`,
  home: map`
.....
.....
.....
.....`
}

let game = false;
const colors = [2, 3, 6, 8, 4, 9]
let interval;

function home(lost) {
  game = false;
  setMap(maps.home)
  setBackground(black)
  addText("Disco", {x: 7, y:2, color: color`2`})
  addText("Press any key!", {x:3,y:13, color:color`2`})
  if (lost) { addText("Game Over", {x:5,y:4, color:color`3`});
                  addText(`Score: ${score}`, {x:5,y:6, color:color`2`})};
  interval = setInterval(() => {
      clearText()
      if (lost) { addText("Game Over", {x:5,y:4, color:color`3`});
                  addText(`Score: ${score}`, {x:5,y:6, color:color`2`})};
      addText("Press any key!", {x:3,y:13, color:color`2`})
      addText("Disco", {x: 7, y:2, color: color`${colors[Math.floor(Math.random()*5)]}`})
  }, 600)
}

function updateScore(score, lives) {
  clearText()
  addText(`Score: ${score}`, {
    x: 0,
    y: 0
  })
  addText(`Lives: ${lives}`, {
    x: 10,
    y: 0
  })
}

const arrows = [w,a,s,d,i,j,k,l]
home(false)
let gameInterval; 
let score = 0;
let lives = 5;
function startGame() {
  score = 0;
  let speed = 1000;
  game = true;
  updateScore(score, lives);
  function gameInterval () {
    // console.log(lives)
    if (lives <= 0) {
      clearText()
      playTune(music.gameOver)
      lives = 5;
      home(true)
    }
    if (game && lives > 0) {
      let index = Math.floor(Math.random()*8)
      addSprite(index<=3?index:index+2 ,0, arrows[index])
      getAll().forEach(e => {
        if(e.type != "R" && e.type != "B" && e.type != "C") {
          if (e.y == height() -1) {
            e.remove()
          }
          e.y++;
        }
      })
      if (speed >= 350) {
        speed -= 5;
      }
      // console.log(speed)
      setTimeout(gameInterval, speed)
    }
  }
  gameInterval();
}

function keyFunction(tilex, game) {
  if (!game) {
    setBackground()
    setMap(maps.game)
    clearInterval(interval);
    clearText()
    startGame()
  } else {
    let tile = getTile(tilex,6)
    if(tile.length == 2){
      let tiletype = tile[0].type
      tile[0].remove()
      score += 1;
      updateScore(score, lives)
      // console.log(score)
      playTune(music[tiletype])
    } else {
      if (lives > 0) {
        lives -= 1;
      }
      updateScore(score, lives)
    }
  }
}


onInput("w", () => {keyFunction(0, game)});
onInput("a", () => {keyFunction(1, game)});
onInput("s", () => {keyFunction(2, game)})
onInput("d", () => {keyFunction(3, game)});
onInput("i", () => {keyFunction(6, game)});
onInput("j", () => {keyFunction(7, game)});
onInput("k", () => {keyFunction(8, game)});
onInput("l", () => {keyFunction(9, game)});